package com.nq.service.impl;

/**

 * 开发者----股牛网络

 * 联系TG：gncms

 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;
import com.nq.common.SystemVersionEnum;
import com.nq.dao.stock.StIpoMapper;
import com.nq.pojo.StockSubscribe;
import com.nq.service.IStockSubscribeService;
import com.nq.dao.StockSubscribeMapper;
import com.nq.stock.entity.StIpo;
import com.nq.utils.DateTimeUtil;
import com.nq.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* @author 11527
* @description 针对表【stock_subscribe(新股)】的数据库操作Service实现
* @createDate 2022-10-24 23:27:27
*/
@Service
@Slf4j
public class StockSubscribeServiceImpl extends ServiceImpl<StockSubscribeMapper, StockSubscribe>
    implements IStockSubscribeService {


    @Value("${spring.profiles.active}")
    private String active;

    @Autowired
    StockSubscribeMapper stockSubscribeMapper;
    @Resource
    private StIpoMapper stIpoMapper;

/**
* @Description: 用户新股列表
* @Param:
* @return:
* @Author: tf
* @Date: 2022/10/25
*/
    @Override
    public ServerResponse<PageInfo> stockSubscribeList( int pageNum, int pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<StIpo> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("open_date", DateUtils.formatDate(new Date()));
        queryWrapper.ge("close_date", DateUtils.formatDate(new Date()));
        queryWrapper.orderByDesc("create_time");
        List<StIpo> stIpos = stIpoMapper.selectList(queryWrapper);
        stIpos.forEach(stIpo -> {
            stIpo.setOpenDate(DateUtils.chToEngDate(stIpo.getOpenDate()));
            stIpo.setCloseDate(DateUtils.chToEngDate(stIpo.getCloseDate()));
            stIpo.setListingDate(DateUtils.chToEngDate(stIpo.getListingDate()));
        });
        PageInfo pageInfo = new PageInfo(stIpos);
        pageInfo.setList(stIpos);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.formatDate(new Date()));
    }

    public static String convertToFormattedDate(String dateStr) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = inputFormat.parse(dateStr);
            return outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ServerResponse pageList(int pageNum, int pageSize, String name, String symbol, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);

        QueryWrapper<StIpo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(StringUtils.isNotBlank(symbol), "symbol", symbol);
        queryWrapper.orderByDesc("create_time");
        List<StIpo> stIpos = stIpoMapper.selectList(queryWrapper);

        PageInfo pageInfo = new PageInfo(stIpos);
        pageInfo.setList(stIpos);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse updateStIpo(StIpo model, HttpServletRequest request) {
        return ServerResponse.createBySuccess(stIpoMapper.updateById(model));
    }

    /**
* @Description: 新增新股
* @Param:
* @return:
* @Author: tf
* @Date: 2022/10/25
*/
    @Override
    public ServerResponse add(StIpo model, HttpServletRequest request) {
        //判断是否已经存在
        log.info("model:{}",model);
        int resultCount = stIpoMapper.insert(model);

//        List<StockSubscribe> stockSubscribeList = this.stockSubscribeMapper.selectList(new QueryWrapper<StockSubscribe>().eq("code", model.getCode()));
//        if (stockSubscribeList != null && stockSubscribeList.size()>0) {
//            return ServerResponse.createByErrorMsg("New stocks already exist. Don't add them repeatedly.");
//        }
//        String sinaStock = SinaStockApi.getSinaStock(model.getStockType()+model.getCode());
//        String[] arrayOfString = sinaStock.split(",");
////        if (arrayOfString.length < 10)
////        return ServerResponse.createByErrorMsg("数据源无该新股");
//        //添加新股
//        int resultCount = this.stockSubscribeMapper.insert(model);
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMsg("Successfully added new shares");
        }
        return ServerResponse.createByErrorMsg("Failed to add new shares");
    }
    /**
    * @Description:  修改新股
    * @Param:
    * @return:
    * @Author: tf
    * @Date: 2022/10/25
    */
    @Override
    public ServerResponse update(StockSubscribe model, HttpServletRequest request) {

        StockSubscribe stockSubscribe = this.stockSubscribeMapper.selectById(model.getNewlistId());
        if (stockSubscribe == null) {
            return ServerResponse.createByErrorMsg("New stocks do not exist.");
        }
        //修改新股
        int resultCount = this.stockSubscribeMapper.updateById(model);
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMsg("Successfully modified new shares");
        }
        return ServerResponse.createByErrorMsg("Failed to modify new shares");
    }
    /**
    * @Description:  删除新股
    * @Param:
    * @return:
    * @Author: tf
    * @Date: 2022/10/25
    */
    @Override
    public ServerResponse del(Integer id, HttpServletRequest request) {

        StIpo ipo = stIpoMapper.selectById(id);
        if (null == ipo) {
            return ServerResponse.createByErrorMsg("New stocks do not exist.");
        }
//        StockSubscribe stockSubscribe = this.stockSubscribeMapper.selectById(id);
//        if (stockSubscribe == null) {
//            return ServerResponse.createByErrorMsg("New stocks do not exist.");
//        }
//        //删除新股
        int resultCount = stIpoMapper.deleteById(id);
//        int resultCount = this.stockSubscribeMapper.deleteById(id);
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMsg("Successful deletion of new shares.");
        }
        return ServerResponse.createByErrorMsg("Failed to delete new shares.");
    }

    /**
     * @Description:  新股抢筹列表
     */
    @Override
    public ServerResponse newStockQc(HttpServletRequest request) {
        String nowDate = DateTimeUtil.stampToDate(String.valueOf(System.currentTimeMillis()));
        List<StockSubscribe> stockSubscribeListQc = this.stockSubscribeMapper.selectList(new QueryWrapper<StockSubscribe>().eq("list_date",nowDate));
        return ServerResponse.createBySuccess(stockSubscribeListQc);
    }



}



